<html>
<head><meta charset="utf-8"><title>Why E0277 is triggered here? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html">Why E0277 is triggered here?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203541463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203541463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203541463">(Jul 10 2020 at 18:23)</a>:</h4>
<p>In the <a href="https://gist.github.com/rust-play/a65d00b78bf20e7492a4c3dcda46e1bb">code snippets</a>, <code>test</code> and <code>test2</code> works fine, but <code>test3</code> cannot get compiled. The error message shows an error of E0277. Why <code>test</code> and <code>test2</code> works fine, but <code>test3</code> failed to compile? It seems that the <code>Option</code> type has an implementation of the <code>From</code> trait, which is implicitly invoked in <code>test</code>.</p>
<div class="codehilite"><pre><span></span><code>   Compiling playground v0.0.1 (/playground)
error[E0277]: `?` couldn&#39;t convert the error to `std::option::NoneError`
  --&gt; src/main.rs:30:6
   |
30 |     r?;
   |      ^ the trait `std::convert::From&lt;std::io::Error&gt;` is not implemented for `std::option::NoneError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required by `std::convert::From::from`
help: consider converting the `Result&lt;T, _&gt;` into an `Option&lt;T&gt;` using `Result::ok`
   |
30 |     r.ok()?;
   |      ^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.
</code></pre></div>



<a name="203543211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203543211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203543211">(Jul 10 2020 at 18:41)</a>:</h4>
<p>It's not clear to me why you think that <code>?</code> should convert a <code>Result</code> to an <code>Option</code>.</p>



<a name="203543561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203543561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203543561">(Jul 10 2020 at 18:44)</a>:</h4>
<p>the compiler said that question mark invoke <code>From</code> trait implicitly, but the behavior is not the same.</p>



<a name="203543662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203543662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203543662">(Jul 10 2020 at 18:45)</a>:</h4>
<p><a href="https://doc.rust-lang.org/src/core/option.rs.html#1354">https://doc.rust-lang.org/src/core/option.rs.html#1354</a></p>



<a name="203543836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203543836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203543836">(Jul 10 2020 at 18:47)</a>:</h4>
<p>I think the magic happens in <code>test</code> is the invocation of <a href="https://doc.rust-lang.org/src/core/task/poll.rs.html#112">From</a> impl of <code>Poll</code>.</p>



<a name="203543893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203543893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203543893">(Jul 10 2020 at 18:47)</a>:</h4>
<p><code>?</code> does invoke <code>From</code>, but what <code>From</code> implementation do you expect to be invoked to turn a <code>Result</code> to <code>Option</code>?</p>



<a name="203544066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203544066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203544066">(Jul 10 2020 at 18:48)</a>:</h4>
<p>I also don't understand what you are attempting to demonstrate between the three functions.</p>



<a name="203544144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203544144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203544144">(Jul 10 2020 at 18:49)</a>:</h4>
<p>in function <code>test</code>, question mark converts Err(...) to <code>Some(Err(...))</code>, but <code>test3</code> failed to compile.</p>



<a name="203544447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203544447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203544447">(Jul 10 2020 at 18:52)</a>:</h4>
<p>See also <a href="https://doc.rust-lang.org/std/ops/trait.Try.html#impl-Try-2">https://doc.rust-lang.org/std/ops/trait.Try.html#impl-Try-2</a> and <a href="https://doc.rust-lang.org/std/ops/trait.Try.html#impl-Try-3">https://doc.rust-lang.org/std/ops/trait.Try.html#impl-Try-3</a></p>



<a name="203545904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203545904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203545904">(Jul 10 2020 at 19:03)</a>:</h4>
<p>This is not the correct doc. I'm still seeking the answer.</p>



<a name="203550947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203550947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203550947">(Jul 10 2020 at 19:50)</a>:</h4>
<p>After some reading, I learned that the <code>Try</code> trait is the place where the magic happened. It's a feature of the <a href="https://github.com/rust-lang/rfcs/blob/master/text/1859-try-trait.md#support-interconversion-but-with-caution">RFC-1859</a>. Thanks  <span class="user-mention" data-user-id="116155">@Jake Goulding</span> ;)</p>



<a name="203551331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203551331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yan <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203551331">(Jul 10 2020 at 19:55)</a>:</h4>
<p>The only problem left is, <code>Try</code> trait is a nightly feature and why I got effected in stable rust?</p>



<a name="203552132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203552132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203552132">(Jul 10 2020 at 20:02)</a>:</h4>
<p>Implementing Try is unstable, but the standard library is allowed to use stable features.</p>



<a name="203574581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203574581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203574581">(Jul 11 2020 at 01:43)</a>:</h4>
<p><span class="user-mention" data-user-id="320287">@Yan</span> specifically, <code>?</code> invokes <code>From</code> on the error type, i.e. between <code>E1</code> and <code>E2</code> in <code>Result&lt;T, E1&gt;</code> vs <code>Result&lt;T, E1&gt;</code>, not on the whole type</p>



<a name="203574583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20E0277%20is%20triggered%20here%3F/near/203574583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20E0277.20is.20triggered.20here.3F.html#203574583">(Jul 11 2020 at 01:43)</a>:</h4>
<p>so it's like <code>.map_err(From::from)</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>